<template lang="">
  <div><!-- 权限点数据展示 -->
    <el-tree
      ref="refTree"
      :data="permissionData"
      :props="{ label: 'name' }"
      :default-expand-all="true"
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <div style="margin-top: 20px; text-align:right">
      <el-button size="small" @click="closetk">取消</el-button>
      <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
import { getRoleDetail, assignPrem } from '@/api/setting'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      permissionData: [] // 存储权限数据
    }
  },
  created() {
    this.loadPermissionList()
    this.loadPermissionByRoleId()
  },
  methods: {
    async loadPermissionList() {
      // 发送请求, 获取权限列表
      const { data } = await getPermissionList()
      console.log('权限列表的数据是', data)
      this.permissionData = tranListToTreeData(data)
    },
    hCancel() {
    // 通过父组件去关闭弹层
      this.$emit('close')
    },
    // 获取角色详情
    async loadPermissionByRoleId() {
      // 根据roleId获取当前这个角色已经具备的权限
      const res = await getRoleDetail(this.roleId)
      console.log('获取当前角色已有的权限点数据', res)
      this.permIds = res.data.permIds
      // 数据回显
      this.$refs.refTree.setCheckedKeys(this.permIds)
    },
    async hSubmit() {
      console.log(this.$refs.refTree.getCheckedKeys())
      this.permIds = this.$refs.refTree.getCheckedKeys()
      await assignPrem({
        id: this.roleId,
        permIds: this.$refs.refTree.getCheckedKeys()
      })
      this.$emit('update:showDialogAssign', false)
    },
    closetk() {
      this.$emit('close')
    }
  }
}
</script>
<style lang="">

</style>
